import {Component, OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {fadeIn} from "../animations/fade-in";
import {NgbPagination} from '@ng-bootstrap/ng-bootstrap';
import {FormControl} from "@angular/forms";
import  'rxjs/Rx';
import {ExamListService} from "./exam-list.service";

@Component({
  selector: 'app-exam-list',
  templateUrl: './exam-list.component.html',
  styleUrls: ['./exam-list.component.css'],
  animations: [fadeIn]
})
export class ExamListComponent implements OnInit {

  public page: number = 1;
  public reportsSize: number = 1;
  public reports: Array<Report>;
  public now: Date = new Date();

  public subjectFilter: FormControl = new FormControl();
  public gradeFilter: FormControl = new FormControl();
  public classFilter: FormControl = new FormControl();
  public typeFilter: FormControl = new FormControl();
  public fromFilter: FormControl = new FormControl();
  public toFilter: FormControl = new FormControl();

  public subjectKW: string = '';
  public gradeKW: string = '';
  public classKW: string = '';
  public typeKW: string = '';
  public fromKW: string;
  public toKW: string;

  constructor(public router: Router,public examListSer: ExamListService) {
    console.log(this.now.getTime());
    this.reports = [
      new Report(1, '江苏省宝应中学高三双周测试2', new Date(1496886833297), '高一', '2班', '月考', '语文，数学，外语，物理，政治，地理，历史，生物'),
      new Report(2, '江苏省宝应中学高三双周测试3', new Date(1496800116618), '高一', '2班', '月考', '语文，数学，外语，物理，政治，地理，历史，生物'),
      new Report(3, '江苏省宝应中学高三双周测试4', new Date(1496800116618), '高一', '2班', '月考', '数学，外语，物理，政治，地理，历史，生物'),
      new Report(4, '江苏省宝应中学高三双周测试5', new Date(1496800116618), '高一', '2班', '月考', '语文，数学，外语，物理，政治，地理，历史，生物'),
      new Report(5, '江苏省宝应中学高三双周测试6', new Date(1496800116618), '高一', '2班', '月考', '外语，物理，政治，地理，历史，生物'),
      new Report(6, '江苏省宝应中学高三双周测试7', new Date(1496800116618), '高一', '2班', '月考', '语文，数学，外语，物理，政治，地理，历史，生物'),
      new Report(7, '江苏省宝应中学高三双周测试8', new Date(1496800116618), '高一', '2班', '月考', '语文，数学，物理，政治，地理，历史，生物'),
      new Report(8, '江苏省宝应中学高三双周测试9', new Date(1496800116618), '高一', '2班', '月考', '语文，数学，物理，政治，地理，历史，生物'),
      new Report(9, '江苏省宝应中学高三双周测试10', new Date(1496800116618), '高一', '2班', '月考', '语文，外语，物理，政治，地理，历史，生物'),
      new Report(10, '江苏省宝应中学高三双周测试11', new Date(1296800116618), '高一', '2班', '月考', '外语，物理，政治，地理，历史，生物'),
      new Report(11, '江苏省宝应中学高三双周测试12', new Date(1196800116618), '高一', '2班', '月考', '外语，物理，政治，地理，历史，生物')
    ];
    this.reportsSize = this.reports.length;
  }

  ngOnInit() {
    this.subjectFilter.valueChanges.debounceTime(500).subscribe(val => this.subjectKW=val);
    this.gradeFilter.valueChanges.debounceTime(500).subscribe(val => this.gradeKW=val);
    this.classFilter.valueChanges.debounceTime(500).subscribe(val => this.classKW=val);
    this.typeFilter.valueChanges.debounceTime(500).subscribe(val => this.typeKW=val);
    this.fromFilter.valueChanges.debounceTime(500).subscribe(val => this.fromKW=val);
    this.toFilter.valueChanges.debounceTime(500).subscribe(val => this.toKW=val);
    this.examListSer.getExamList(this.page).subscribe((e) =>{
      let data = e.json();
      //console.log(data);
      this.page = data.currentPage;
      this.reports = data.data.map((item,count,arry)=>{
        let temp: string = arry[count].create;
        arry[count].create = new Date(temp);
        return arry[count];
      });
      this.reportsSize = data.maxPage;
    });
  }
  pageChange(){
    //console.log(this.page);
    this.examListSer.getExamList(this.page).subscribe((e) =>{
      let data = e.json();
      //console.log(data);
      this.page = data.currentPage;
      this.reports = data.data.map((item,count,arry)=>{
        let temp: string = arry[count].create;
        arry[count].create = new Date(temp);
        return arry[count];
      });
      this.reportsSize = data.maxPage;
    });
  }
}
export class Report {
  constructor(public id: number,
              public name: string,
              public create: Date,
              public gradeName: string,
              public className: string,
              public type: string,
              public subject: string) {
  }
}
